home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 076-100 / disk_085 / csh / rawconsole.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  5KB  |  231 lines

  1. /*
  2.  * RawConsole.c
  3.  *
  4.  * Shell 2.06M    28-May-87
  5.  * console handling, command line editing support for Shell
  6.  * using new console packets from 1.2.
  7.  * Written by Steve Drew. (c) 14-Oct-86.
  8.  * 16-Dec-86 Slight mods to rawgets() for Disktrashing.
  9.  *
  10.  */
  11.  
  12. extern int aux; /* for use with aux: */
  13.  
  14. #include "shell.h"
  15.  
  16. void
  17. setraw(onoff)
  18. {
  19.     if (onoff) set_raw();
  20.     else set_con();
  21. }
  22.  
  23. char *
  24. rawgets(line,prompt)
  25. char *line, *prompt;
  26. {
  27.     char *get_var();
  28.     char *gets();
  29.     register int n, pl;
  30.     register int max, i;
  31.     unsigned char c1,c2,c3;
  32.     char fkeys[5];
  33.     char *s;
  34.     int fkey;
  35.     int insert = 1;
  36.     char rep[14];
  37.     static int width;
  38.     int recall = -1;
  39.     struct HIST *hist;
  40.  
  41.     if (aux) {
  42.     printf("%s",prompt);
  43.     fflush(stdout);
  44.     }
  45.     if (!IsInteractive(Input()) || aux ) return(gets(line));
  46.     if (WaitForChar((long)Input(), 100L) ||   /* don't switch to 1L ...*/
  47.        stdin->_bp < stdin->_bend) {        /* else causes read err's*/
  48.     /*    printf("%s",prompt); */
  49.     gets(line);
  50.     return(line);
  51.     }
  52.     setraw(1);
  53.     printf("%s",prompt);
  54.     max = pl = i = strlen(prompt);
  55.     strcpy(line,prompt);
  56.     if (!width) width = 77;
  57.     if (s = get_var (LEVEL_SET, "_insert"))
  58.     insert = atoi(s) ? 1 : 0;
  59.  
  60.     while((c1 = getchar()) != 255) {
  61.     if (c1 < 156) switch(c1) {
  62.         case 155:
  63.          c2 = getchar();
  64.          switch(c2) {
  65.              case 'A':            /* up arrow   */
  66.             n = ++recall;
  67.              case 'B':            /* down arrow */
  68.             line[pl] = '\0';
  69.             if (recall >= 0 || c2 == 'A') {
  70.                 if (c2 == 'B') n = --recall;
  71.                 if (recall >= 0) {
  72.                 for(hist = H_head; hist && n--;
  73.                     hist = hist->next);
  74.                 if (hist) strcpy(&line[pl],hist->line);
  75.                 else recall = H_len;
  76.                 }
  77.             }
  78.             if (i != pl)
  79.                 printf("\233%dD",i);
  80.             printf("\015\233J%s",line);
  81.             i = max = strlen(line);
  82.             break;
  83.              case 'C':            /* right arrow*/
  84.             if (i < max) {
  85.                 i++;
  86.                 printf("\233C");
  87.             }
  88.             break;
  89.              case 'D':            /* left arrow */
  90.             if (i > pl) {
  91.                 i--;
  92.                 printf("\233D");
  93.             }
  94.             break;
  95.             case 'T':            /* shift up   */
  96.             case 'S':            /* shift down */
  97.             break;
  98.             case ' ':            /* shift -> <-*/
  99.             c3 = getchar();
  100.             break;
  101.             default:
  102.             c3 = getchar();
  103.             if (c3 == '~') {
  104.                 fkey = c2;
  105.                 fkeys[0] = 'f';
  106.                 if (c2 == 63) {
  107.                 strcpy(&line[pl],"help");
  108.                 goto done;
  109.                 }
  110.             }
  111.             else if (getchar() != '~') { /* window was resized */
  112.                 while(getchar() != '|');
  113.                 printf("\2330 q"); /* get window bounds */
  114.                 n = 0;
  115.                 while((rep[n] = getchar()) != 'r' && n++ < 14 );
  116.                 width = (rep[n-3] - 48) * 10 + rep[n-2] - 48;
  117.                 rep[n-1] = '\0';
  118.                 set_var (LEVEL_SET, "_width", &rep[n-3]);
  119.                 break;
  120.             }
  121.             else {
  122.                 fkey = c3;
  123.                 fkeys[0] = 'F';
  124.             }
  125.             sprintf(fkeys+1,"%d",fkey - 47);
  126.             if (!(s = get_var(LEVEL_SET, fkeys))) break;
  127.             strcpy(&line[pl], s);
  128.             printf("%s",&line[pl]);
  129.             goto done;
  130.             break;
  131.             }
  132.         break;
  133.         case 8:
  134.         if (i > pl) {
  135.             i--;
  136.             printf("\010");
  137.         }
  138.         else break;
  139.         case 127:
  140.         if (i < max) {
  141.             int j,t,l = 0;
  142.             movmem(&line[i+1],&line[i],max-i);
  143.             --max;
  144.             printf("\233P");
  145.             j = width - i % width - 1;     /* amount to end     */
  146.             t = max/width - i/width;     /* no of lines          */
  147.             for(n = 0; n < t; n++) {
  148.             l += j;             /* no. of char moved */
  149.             if (j) printf("\233%dC",j); /* goto eol          */
  150.             printf("%c\233P",line[width*(i/width+n+1)-1]);
  151.             j = width-1;
  152.             }
  153.             if (t)
  154.             printf("\233%dD",l+t);   /* get back */
  155.         }
  156.         break;
  157.         case 18:
  158.         n = i/width;
  159.         if (n) printf("\233%dF",n);
  160.         printf("\015\233J%s",line);
  161.         i = max;
  162.         break;
  163.         case 27:
  164.         case 10:
  165.         break;
  166.         case 1:
  167.         insert ^= 1;
  168.         break;
  169.         case 21:
  170.         case 24:
  171.         case 26:
  172.         if (i > pl)
  173.             printf("\233%dD",i-pl);
  174.         i = pl;
  175.         if (c1 == 26) break;
  176.         printf("\233J");
  177.         max = i;
  178.         line[i] = '\0';
  179.         break;
  180.         case 11:        /* ^K */
  181.         printf("\233J");
  182.         max = i;
  183.         line[i] = '\0';
  184.         break;
  185.         case 28:        /* ^\ */
  186.         setraw(0);
  187.         return(NULL);
  188.         case 5:
  189.         printf("\233%dC",max - i);
  190.         i = max;
  191.         break;
  192.         case 13:
  193.         line[max] = '\0';
  194. done:        printf("\233%dC\n",max - i);
  195.  
  196.         setraw(0);
  197.         strcpy(line, &line[pl]);
  198.         return(line);
  199.         default:
  200.         if (c1 == 9) c1 = 32;
  201.         if (c1 > 31 & i < 256) {
  202.             if (i < max && insert) {
  203.             int j,t,l = 0;
  204.             movmem(&line[i], &line[i+1], max - i);
  205.             printf("\233@%c",c1);
  206.             t = max/width - i/width;
  207.             j = width - i % width - 1;
  208.             for(n = 0; n < t; n++) {
  209.                 l += j;
  210.                 if (j) printf("\233%dC",j);
  211.                 printf("\233@%c",line[width*(i/width+n+1)]);
  212.                 j = width-1;
  213.             }
  214.             if (t) printf("\233%dD",l + t);
  215.             ++max;
  216.             }
  217.             else {
  218.             if (i == pl && max == i) printf("\015%s",line);
  219.             putchar(c1);
  220.             }
  221.             line[i++] = c1;
  222.             if (max < i) max = i;
  223.             line[max] = '\0';
  224.         }
  225.     }
  226.     }
  227.     setraw(0);
  228.     return(NULL);
  229. }
  230.  
  231.